package IAPractica1;

import java.util.ArrayList;
import java.util.Vector;

import IA.Gasolina.Gasolineras;

public class Delivery {
	/*Variables de classe*/
	private static final int max = 2;
	private static Gasolineras gasolineras;
	private static ArrayList<Integer> gasAsociada;
	
	/*Variables*/
	private Vector<Integer> parades;
	private int distance;
	private int xOrigen;
	private int yOrigen;
	
	/*Funcions privades*/
		
	public Gasolineras getGas(){
		return gasolineras;		
	}
	
	private int findPeticio(int idPeticio){
		for(int i = 0; i<max; ++i)
			if(parades.get(i)==idPeticio) return i;
		return -1;
	}
	
	private int getFirst(){
		for(int i = 0; i<max; ++i)
			if(parades.get(i)!=-1) return parades.get(i);
		return -1;
	}
	
	private int getSecond(){
		for(int i = max-1; i>=0; --i)
			if(parades.get(i)!=-1) return parades.get(i);
		return -1;
	}
	
	private int dist(int x1, int y1, int x2, int y2){
		return Math.abs(x1- x2) + Math.abs(y1- y2);	
	}
	
	private void calculateDistance(){
		// eze: petaba (solved)
		int first = getFirst();
		
		if(first == -1) distance = 0;
		
		else {
			
			int second = getSecond();
			if (second == -1) distance = 0;
			else {
				if(first == second){
					int xGas = gasolineras.get(gasAsociada.get(first)).getCoordX();
					int yGas = gasolineras.get(gasAsociada.get(first)).getCoordY();
					distance = 2*dist(xOrigen, yOrigen, xGas,yGas);
				}
				else{
					int xGas1 = gasolineras.get(gasAsociada.get(first)).getCoordX();
					int yGas1 = gasolineras.get(gasAsociada.get(first)).getCoordY();
					int xGas2 = gasolineras.get(gasAsociada.get(second)).getCoordX();
					int yGas2 = gasolineras.get(gasAsociada.get(second)).getCoordY();
					distance = dist(xOrigen, yOrigen, xGas1,yGas1); 
					distance += dist(xGas1,yGas1, xGas2,yGas2); 
					distance += dist(xGas2,yGas2,xOrigen, yOrigen); 
				}	
			}
		}
	}
	
	/*Funcions publiques*/
	
	@SuppressWarnings("static-access")
	Delivery ( ArrayList<Integer> gasAsociada, Gasolineras gasolineras){
		this.gasolineras = gasolineras;
		this.gasAsociada = gasAsociada;
	}
	
	Delivery(int xOrigen, int yOrigen){//, ArrayList<Integer> gasAsociada, Gasolineras gasolineras){		
		//this.gasAsociada = gasAsociada;
		//this.gasolineras = gasolineras;
		this.xOrigen = xOrigen;
		this.yOrigen = yOrigen;
		
		parades = new Vector<Integer>(max);	
		for(int i = 0; i<max; ++i) 
			parades.add(-1);
	}	
	
	Delivery(int xOrigen, int yOrigen, int distance, Vector<Integer> parades){
		this.xOrigen = xOrigen;
		this.yOrigen = yOrigen;
		this.distance = distance;
		this.parades = parades;		
	}	
	
	public void elimParada(int idPeticio){
		int index = findPeticio(idPeticio);
		if(index!=-1)parades.set(index,-1);
	}
	
	public void afegirParada(int valor){
		int index = findPeticio(-1);
		parades.set(index,valor);
	}
	
	public void subsParada(int oldValue, int newValue){
		int index = findPeticio(oldValue);
		parades.set(index,newValue);
	}
	
	public boolean present(int idPeticio){
		return findPeticio(idPeticio) != -1;
	}
	
	public int getDistance() {
		calculateDistance();
		return distance;
	}
	
	public Vector<Integer> getParades(){
		return parades;
	}
	
	public boolean full(){
		return (findPeticio(-1) == -1);
	}
	
	public void getidEntregues(ArrayList<Integer> list){
		for(int i = 0; i<max; ++i){
			if (parades.get(i) != -1)
				list.set(parades.get(i), i);
		}
	}
	
	public void validarPeticions(ArrayList<Integer> list, int centre){
		// eze
		for(int i = 0; i<max; ++i){
			int idPet = parades.get(i);
			if (idPet != -1) // Tenemos que comprobar que el id de la peticion no sea -1
				list.set(parades.get(i), centre);
		}
	}	
	
	public Delivery copy(){
		Vector<Integer> paradesCopy = new Vector<Integer> ();
		for(int i = 0; i<max; ++i)
			paradesCopy.add(parades.get(i));
		return new Delivery(xOrigen, yOrigen, distance, paradesCopy);
	}
}
